package per.zhang.serverPrevioder.config;

import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Component;
import per.zhang.serverPrevioder.base.utils.AppointmentStateCode;
import per.zhang.serverPrevioder.entity.Appointment;
import per.zhang.serverPrevioder.service.AppointmentService;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;
import java.util.List;

/**
 * @auther zhang
 * @ TO DO  每天将没有接种疫苗的预定进行过期处理
 * @creatTime 2021/1/27 21:38
 **/
@Slf4j
@Component
public class DateUpdateConfig {

    @Autowired
    private AppointmentService appointmentService;

    @Scheduled(cron = "0 0 0 * * ? ")
    public void updateMessageStatus() throws Exception {
        log.info("更新执行");
        SimpleDateFormat sdf =new SimpleDateFormat("yyyy-MM-dd 00:00:00" );
        Date date = new Date();
        String dateTime = sdf.format(date);
        Calendar c = Calendar.getInstance();
        c.setTime(sdf.parse(dateTime));
        c.add(Calendar.DAY_OF_MONTH, -1);
        Date yesterday = c.getTime();

        Appointment appointment = new Appointment();
        appointment.setAppointmentTime(yesterday);
        appointment.setState(AppointmentStateCode.APPOINTING);
        List<Appointment> appointmentList = appointmentService.findByParams(appointment);
        for (Appointment item : appointmentList) {
            item.setState(AppointmentStateCode.OVERDUE);
            log.info("该预约已过期"+item.toString());
            appointmentService.update(item);
        }
        log.info("更新完成");
    }

}
